package com.zbec.mdrs.web;

import com.zbec.framework.core.entity.SystemDictionary;
import com.zbec.framework.core.entity.SystemDictionaryType;
import com.zbec.framework.core.service.SystemCodeTableService;
import com.zbec.framework.core.service.SystemDictionaryService;
import com.zbec.framework.core.service.SystemDictionaryTypeService;
import com.zbec.framework.core.vo.LabelValue;
import com.zbec.framework.security.vo.UserVO;
import com.zbec.framework.core.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by fxl on 2015/11/8.
 */
public class BaseController {

    @Autowired
    private SystemDictionaryService systemDictionaryService;
    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;
    @Autowired
    private SystemCodeTableService systemCodeTableService;

    public String getIPAddress( HttpServletRequest req) {
        String lastLoginIP = req.getRemoteAddr();

        if (lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {
            lastLoginIP = req.getHeader("Proxy-Client-IP");
        }
        if (lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {
            lastLoginIP = req.getHeader("WL-Proxy-Client-IP");
        }
        return lastLoginIP;
    }

    public boolean isUserReseted(UserVO userVo){
        String value="";
        SystemDictionaryType modifyPassword = this.systemDictionaryTypeService.findCodeTypeByTypeCode(Constants.CODE_TYPE_USER_MODIFY_PASSWORD);
        List<SystemDictionary> modifyPasswordValues = this.systemDictionaryService.findCodeIterableByCodeType(modifyPassword);
        if (modifyPasswordValues != null && !modifyPasswordValues.isEmpty()) {
            SystemDictionary modifyPasswordCode = modifyPasswordValues.get(0);
            value=modifyPasswordCode.getCodeValue();
        }

        if(("Y").equals(value)&& ("1").equals(userVo.getReseted())){
            return true;
        }
        return false;
    }

    public String  findDicById(String codeTypeId){
        List<LabelValue> list= systemCodeTableService.codeTable("T_SYSTEM_DICT","CODE_TYPE_ID = "+codeTypeId);
        String s="";
        for (int i = 0; i <list.size() ; i++) {
            String value=list.get(i).getValue();
            String label=list.get(i).getLabel();
            s=s+value+":"+label+";";
        }
        if(s!="" && s.length()>0){
            s=s.substring(0,s.length()-1);
        }
        return s;
    }

}
